<?php
namespace app\api\controller\v1;
use app\api\logic\HuiLogic;
use app\api\logic\Rsa2;
use app\service\PaymentService;
use think\Controller;
use think\Db;
use think\Request;
class Hui extends Base
{
    public function _initialize()
	{
		parent::_initialize();
	}

    public function phpinfo()
    {
        echo phpinfo();
        exit();
    }



    /***
    上传图片返回图片地址
     **/
    public function upload_img(){
//        $file = ROOT_PATH."/public/img/192.png";
        $file = ROOT_PATH."/public/img/chizheng.jpg";
        $result = HuiLogic::uplode($file);
        $return = json_decode($result,1);
        if ($return['code'] == 10000){
            $url = $return['biz_content']['file_id'];
            $insert = [
                'content'=>$url,
                'add_time'=>time()
            ];
            Db::name('content')->insert($insert);
            echo '上传成功,地址:'.$url;
        }
    }

    /**
    本地保存图片
     */
    public function upload_bd(Request $request){
        $file = $request->file();
        $pwd = ROOT_PATH."/public/hfb_img/";
        $result = HuiLogic::upload_bd($file,$pwd);
        dump($result);
    }

    /**
    自动获取分账单号
     */
    public function return_order_no(Request $request){
        $param = $request->param();
        $insert['content'] = json_encode($param,JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        $insert['add_time'] = time();
        Db::name('content')->insert($insert);
        $param['encrypt_data'] = '{"agent_id":"2154762","ref_agent_id":"2169937","encrypt_data":"AAgHM4/NVA7R44bh9pVwJKt1CimEWK8ZvlMs++p7SmvnYIk8Xpu2/zq4/WYA6zP7cIRuvPJMH/6gJhCNwuBE66vN+9WK5tC1xAT5D8YmkjTKucTjUkkqpnzwoWwOe8OsZzZl5CwSGaU6BE/vCnInFk+REteUawBMO6GJIoboYhuA8kVs1KdOXfX7cCw7LaRqm22EfTgFQFLe2hG36FV4woI6jnzpWD54k6susSfXqrdjH8+szmEaHQGTQhGKqkGiIO6DsaXVeEsHuVgj2341bpEX8+vVZX7O2p8OndtKcavhh/8kkdDeMtWeYces8v8j1PZufmv7Bs6k0rT+xtGc7A==","sign":"fzWmwHfstqUj4NCM7+tcGYSLfd/qZpJVFUPAv3yAwuwZTKtQWR1zQ1rw6sLSTdo2jPqZaGolW67quQWbP3te5qC6XZare8OjatKfj4ObFfrgX8pN3bJr88ZWjBv1TABibgeV9S3jsZERJMap3EP5uxjdXXvET/FuAA8ul7SWX/GSDGg30WSe2Ip528eTpDvP7YN6uG0Coi0LIUIhpPZP9jDmCRV6/PTmL4GVTe0uzlfSEyN7piURimXJ/akPQo6JcdbXRc5ga2E0EFeWpnMRpArra4/DK2aNJV8Jnz4bORLS/ymgIxtQ+2i3qZ7+Ca8sdNnSqdo8+n6emSAVgfYXAQ=="}';
        $info = HuiLogic::jiemi_n($param['encrypt_data']);
        $result = Db::name('fenzhang_hao_list')->insertGetId($info);
        if ($result){
            return 'ok';
        }else{
            return 'error';
        }

    }

    public function fix(){
        $str = 'AAgHM4/NVA7R44bh9pVwJKt1CimEWK8ZvlMs++p7SmvnYIk8Xpu2/zq4/WYA6zP7cIRuvPJMH/6gJhCNwuBE66vN+9WK5tC1xAT5D8YmkjTKucTjUkkqpnzwoWwOe8OsZzZl5CwSGaU6BE/vCnInFk+REteUawBMO6GJIoboYhuA8kVs1KdOXfX7cCw7LaRqm22EfTgFQFLe2hG36FV4woI6jnzpWD54k6susSfXqrdjH8+szmEaHQGTQhGKqkGiIO6DsaXVeEsHuVgj2341bpEX8+vVZX7O2p8OndtKcavhh/8kkdDeMtWeYces8v8j1PZufmv7Bs6k0rT+xtGc7A==';
        $info = HuiLogic::jiemi_n($str);
        dump($info);
    }



    /**
    小微店铺入驻
     */
    public function add_shop(){
        $biz_content = [
            'bank_account_name'=>'吉晓彬',
            'bank_account_type'=>'2',
            //银行卡背面
            'bank_back_img'=>'/public/01/20230920/a1429f1307a14d24c3403c803f39d598.jpg',
            'bank_card_no'=>'6214853513718677',
            //银行卡正面
            'bank_front_img'=>'/public/01/20230920/f7aea0e332dd141d133d24e9baae1ba1.jpg',
            'bank_name'=>'招商银行',
            //手持银行卡照片
            'bank_owner_hold_img'=>'/public/01/20230920/5a41a8d3426e4c798649816d0f6010c2.jpg',
            'bank_region_code'=>'140107',
            'business_category_code'=>'1000',
            'contact_email'=>'1830635570@qq.com',
            'customer_type'=>3,
            'customer_name'=>'吉晓彬',
            'if_sign'=>1,
            'manage_name'=>'吉晓彬',
            'manage_id_card'=>'142430199109301637',
            'manage_id_card_valid_type'=>'1',
            'manage_id_card_valid_begin'=>'2019-03-20',
            'manage_id_card_valid_end'=>'2039-03-20',
            'manage_mobile'=>'13100008431',
            'manage_region_code'=>'140105',
            'manage_detail_address'=>'山西省太原市小店区长风街道和信商座3202',
            //身份证正面
            'manage_id_card_front_img'=>'/public/01/20230915/a72dd36a001b43d75379d003c6090066.jpg',
            //身份证背面
            'manage_id_card_back_img'=>'/public/01/20230915/c366c5f4b3a0685f947b261e6687253e.jpg',
            //持身份证照片
            'manage_hold_id_card_front_img'=>'/public/01/20230920/172fc6b6e491f1bc51799c04c30fc906.jpg',
            //持银行卡照片
            'manage_hold_bank_front_img'=>'/public/01/20230920/5a41a8d3426e4c798649816d0f6010c2.jpg',
            //门头照
            'shop_front_img'=>'/public/01/20230915/63c2761d4273a6662dbb91d7d35abd8e.jpg',
            //店内场景
            'shop_env_img'=>'/public/01/20230915/3c960bc02fbf46fb9b562987090dfaa0.jpg',
            //收银台照片
            'shop_cashier_img'=>'/public/01/20230915/63c2761d4273a6662dbb91d7d35abd8e.jpg',
        ];

        $insert = $biz_content;
        unset($insert['bank_account_name']);
        unset($insert['manage_name']);
        $insert['add_time'] = time();
        $info = Db::name('apply_shop')->where('manage_mobile',$insert['manage_mobile'])->find();
        if (!empty($info) && empty($info['apply_no'])){
            $id = $info['id'];
        }else{
            $id = Db::name('apply_shop')->insertGetId($insert);
        }

        if($id){
            $result = HuiLogic::ruzhu($biz_content);
            $result = json_decode($result,true);
            dump($result);
            if ($result['code'] == 10000){
                $ret = Db::name('apply_shop')->where('id',$id)->update(['apply_no'=>$result['biz_content']['apply_no']]);
                if ($ret){
                    echo '申请中,编号:'.$result['biz_content']['apply_no'];
                }else{
                    echo '申请失败';
                }
            }else{
                echo '申请失败';
            }
        }else{
            echo '数据有误';
        }



    }

    /**
    企业入驻
    返回值
    {
    ["msg"] => string(7) "success"
    ["biz_content"] => array(1) {
    ["apply_no"] => string(18) "230915155255008033"
    }
    ["code"] => int(10000)
    ["sign"] => NULL
    }
     */
        public function add_company(){
        $result = HuiLogic::company();
        $result = json_decode($result,true);
        dump($result);
    }


    /**
     * 返回值

    {
    "msg":"success",
    "biz_content":{
    "apply_no":"230914183247004642"
    },
    "code":10000,
    "sign":null
    }


     ***/


    /**
    查询入驻进度
     *
     * 返回
     * "msg":"success",
    "biz_content":{
    "wallet_id":"",
    "apply_no":"230914183247004642",
    "audit_status":"-1",
    "customer_id":"230914183247004642",
    "login_account":"",
    "heepay_id":"",
    "audit_desc":"入驻驳回:四要素鉴权不通过：不匹配|头像面姓名验证失败|头像面证件号验证失败|国徽面有效期验证失败|银行卡类型验证失败|开户行验证失败|银行卡号验证失败|"
    },
    "code":10000
     *
     *
     * {
    "msg":"success",
    "biz_content":{
    "wallet_id":"",
    "apply_no":"230915155255008033",
    "audit_status":"0",
    "customer_id":"230915155255008033",
    "login_account":"",
    "heepay_id":"",
    "audit_desc":"入驻待审核"
    },
    "code":10000
    }"
     *
     * "msg":"success",
    "biz_content":{
    "wallet_id":"",
    "apply_no":"230915155255008033",
    "audit_status":"3",
    "customer_id":"230915155255008033",
    "login_account":"jixiaobin@hexu2022.vip",
    "heepay_id":"2169937",
    "audit_desc":"商户启用"
    },
    "code":10000
     */

    public function check_add(){
        $mobile = '13100008431';
        $info = Db::name('apply_shop')->where('manage_mobile',$mobile)->find();
        $result = HuiLogic::check_add($mobile);
        $result = json_decode($result);
        if ($result->biz_content->audit_status == 2){
            echo  '审核通过';
            exit();
        }
        if ($result->biz_content->audit_status == 0){
            echo  '待审核';
            exit();
        }
        if ($result->biz_content->audit_status == 3){
            Db::name('apply_shop')->where('id',$info['id'])->update(['is_use'=>1]);
            $shop_info = [
                'apply_no'=>$result->biz_content->apply_no,
                'login_account'=>$result->biz_content->login_account,
                'heepay_id'=>$result->biz_content->heepay_id,
                'audit_status'=>$result->biz_content->audit_status,
                'name'=>$info['customer_name'],
                'manage_id_card'=>$info['manage_id_card'],
                'manage_mobile'=>$info['manage_mobile'],
                'bank_card_no'=>$info['bank_card_no'],
                'add_time'=>time(),
            ];
            Db::name('shop_list')->insert($shop_info);

            echo  '商户启用';
            exit();
        }
        if ($result->biz_content->audit_status == -1){
            Db::name('apply_shop')->where('id',$info['id'])->update(['is_use'=>-1]);

            echo  $result->biz_content->audit_desc;
            exit();
        }

    }


    /**
    调用支付
     **/
    public function h_pay(){
        $pay_data = [
            'order_id' => '24627',
            'agent_bill_id' => '20231021152547773795',
            'pay_amt' => '214.00',
            'notify_url' => 'http://git_mo.com/api/v1/Huifubao/notify_pay?order_no=20231021152547773795',
            'return_url' => 'http://git_mo.com/h5/pages/order/order',
            'goods_name' => '中式推拿',
            'goods_num' => 1,
        ];
        $return = HuiLogic::pay($pay_data);
        halt($return) ;
    }


    /**
      支付回调
     * ***/
    public function pay_back(Request $request){
        $param = $request->param();
        if (!empty($param)){
            Db::name('content')->insert(['content'=>json_encode($param),'add_time'=>time()]);
        }
    }

    /**
    支付完成后跳转接口 查询是否支付成功
     */
    public function pay_backt(Request $request){
        $param = $request->param();
//        $no = $param['no'];
        $id = '2154762';
        $no = '202309200001';
        $result = HuiLogic::pay_backt($id,$no);
        if ($result['result'] == -1){
            echo '支付失败';
        }
        if ($result['result'] == 1){
            echo '支付成功';
        }
        if ($result['result'] == 0){
            echo '支付处理中';
        }
    }


    /**
    分账
     */
    public function deal_money(){
        //组成分账规则
        $allot_data[0] = [
            '0',
            '1830635570@qq.com',
            '9.90',
            'F',
            '服务费',
        ];

        $str = '';
        foreach ($allot_data as $value){
            foreach ($value as $values){
                $str .= $values.'^';
            }
            $str = substr($str,0,-1);
            $str .= '|';
        }
        $str = substr($str,0,-1);

        //组成签名
        $sign = [
            'allot_data'=>$str,
            'batch_no'=>'100001',
            'from_ip'=>implode('_',explode('.',HuiLogic::getIP())),
            'hy_bill_no'=>'Z23092224612701D',
            'ref_agent_id'=>'2169937',
            'version'=>'1',
        ];
        $result = HuiLogic::deal_money($sign);
        echo $result;
    }

    /**
    提现到银行卡
     */
    public function take_money(){
        $result = HuiLogic::take_money();
        dump($result);
        if ($result['return_code'] == 'SUCCESS'){
            //存入提现数据
        }else{
            echo $result['result_msg'];
        }
        dump($result);
    }

    /**
    查看提现记录
     */
    public function check_take(){
        $result = HuiLogic::check_take();
        dump($result);
        if ($result['body']['return_code'] == 'SUCCESS'){
            //修改提现数据
        }else{
            echo $result['body']['result_msg'];
        }
        dump($result);
    }

    public function shou_message(Request $request){
        $info = $request->param();
        \think\log::write('********************');
        \think\log::write($info);
        return json_encode(['code'=>1,'msg'=>'记录']);
    }





}